$(function () {
    $("#jqGrid").jqGrid({
        url: '/tts/sys/menu/list',
        datatype: "json",
        colModel: [
            { label: '菜单ID', name: 'id', width: 40, key: true },
            { label: '菜单名称', name: 'name', width: 60 },
            { label: '上级菜单', name: 'parentName', width: 60 },
            { label: '菜单图标', name: 'icon', width: 50, formatter: function(value, options, row){
                return value == null ? '' : '<i class="'+value+' fa-lg"></i>';
            }},
            { label: '菜单URL', name: 'url', width: 100 },
            { label: '授权标识', name: 'perms', width: 100 },
            { label: '类型', name: 'type', width: 50, formatter: function(value, options, row){
                if(value === "FOLDER"){
                    return '<span class="label label-primary">目录</span>';
                }
                if(value === "MENU"){
                    return '<span class="label label-success">菜单</span>';
                }
                if(value === "BUTTON"){
                    return '<span class="label label-warning">按钮</span>';
                }
            }},
            { label: '排序号', name: 'orderNum', width: 50},
            { label: '状态', name: 'status', width: 50,formatter:function(value,options,row){
                if(value==="VALID"){
                    return '<span class="label label-primary">启用状态</span>';
                }
                if(value==="INVALID"){
                    return '<span class="label label-success">禁用状态</span>';
                }
            }}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "menuId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el:'#rrapp',
    mixins: [mixin],
    data:{
        showList: true,
        title: null,
        errorMsg:'',
        menu:{
            parentName:null,
            parentId:0,
            type:"MENU",
            orderNum:0,
            status:"VALID"
        },
        q:{
            name:null,
            url:'',
            type:'MENU',
            status:'VALID'
        }
    },
    computed:{
        errorShow :function () {
            return this.errorMsg!='';
        }
    },
    methods: {
        search :function(){
            var json = getJSONObjectFromObject(vm.q);
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:json,
                page:page
            }).trigger("reloadGrid");
        },
        getMenu: function(menuId){
            //加载菜单树
            $.get("/tts/sys/menu/select", function(r){
                ztree = $.fn.zTree.init($("#menuTree"), setting, r.menuList);
                var node = ztree.getNodeByParam("id", vm.menu.parentId);
                ztree.selectNode(node);
                vm.menu.parentName = node.name;
            })
        },
        add: function(){
            vm.errorMsg=""
            vm.showList = false;
            vm.title = "新增";
            vm.menu = {parentName:null,parentId:0,type:1,orderNum:0};
            vm.getMenu();
        },
        update: function (event) {
            var menuId = getSelectedRow();
            if(menuId == null){
                return ;
            }

            $.get("/tts/sys/menu/info/"+menuId, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.menu = r.menu;
            });

            vm.getMenu();
        },
        valid:function(event){
            var menuIds = getSelectedRows();
            if(menuIds == null){
                return ;
            }
            var ids = [];
            menuIds.forEach(function(e){
                ids.push({name:"id",value:e})
            })
            confirm('确定要启用选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: "/tts/sys/menu/valid",
                    data: ids,
                    contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        invalid: function (event) {
            var menuIds = getSelectedRows();
            if(menuIds == null){
                return ;
            }
            var ids = [];
            menuIds.forEach(function(e){
                ids.push({name:"id",value:e})
            })
            confirm('确定要禁用选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: "/tts/sys/menu/invalid",
                    data: ids,
                    contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function (event) {
            var url =  "/tts/sys/menu/save"
            var json = getJSONArrayFromObject(vm.menu);
            $.ajax({
                type: "POST",
                url: url,
                contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                data: json,
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        menuTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择菜单",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#menuLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级菜单
                    vm.menu.parentId = node[0].id;
                    vm.menu.parentName = node[0].name;

                    layer.close(index);
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
        }
    }
});